//////////////////////////////////////////////////////////////////////
////                                                              ////
////  JTAGEmulator.v                                              ////
////                                                              ////
////                                                              ////
////  This file is part of the "PICO" project                     ////
////  http://www.picocomputing.com                                ////
////                                                              ////
////                                                              ////
//////////////////////////////////////////////////////////////////////
////                                                              ////
//// Copyright (C) 2004, Pico Computing, Inc.                     ////
//// http://www.picocomputing.com                                 ////
////                                                              ////
//// This source file may be used and distributed without         ////
//// restriction provided that this copyright statement is not    ////
//// removed from the file and that any derivative work contains  ////
//// the original copyright notice and the associated disclaimer. ////
////                                                              ////
//// This source file is free software; you can redistribute it   ////
//// and/or modify it under the terms of the GNU Lesser General   ////
//// Public License as published by the Free Software Foundation; ////
//// either version 2.1 of the License, or (at your option) any   ////
//// later version.                                               ////
////                                                              ////
//// This source is distributed in the hope that it will be       ////
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
//// PURPOSE.  See the GNU Lesser General Public License for more ////
//// details.                                                     ////
////                                                              ////
//// You should have received a copy of the GNU Lesser General    ////
//// Public License along with this source; if not, download it   ////
//// from http://www.opencores.org/lgpl.shtml                     ////
////                                                              ////
//JF//////////////////////////////////////////////////////////////DH//
																	 
//This code emulates a PC parallel port with a JTAG Parallel Port Cable connected to it
//The JTAG Parallel Port Cable is connected to all 3 JTAG enabled devices on the Pico, which 
//are, the FPGA, CPLD and Ethernet (in that order).

`include "PicoDefines.v"

module JTAGEmulator(enable, addr, din, dout, read, write, tck, tdi, tdo, tms);

input enable;                                                      //Master Enable Signal
input [1:0] addr;                                                  //IO Address Bus
input [7:0] din;	                                                 //Data In from Host
output [15:0] dout;                                                //Data Out from Host
input read;                                                        //IO Read
input write;                                                       //IO Write
output tck;                                                        //JTAG Clock
output tdi;                                                        //JTAG Data In
input tdo;                                                         //JTAG Data Out
output tms;                                                        //JTAG Mode Select

reg [7:0] lpt_data;                                                //LPT D0-D7
reg [3:0] lpt_ctrl;                                                //LPT Control Register

wire [7:0]dout_data;
wire [7:0]dout_status;
wire [7:0]dout_ctrl;
wire [7:0]lpt_stat_out;

wire prog_line;
wire comb_line;

wire din_line;
wire ctrl_line;
wire clk_line;
wire tms_line;

wire read_buf;

//synthesis attribute init lpt_data 11111111;
//synthesis attribute init lpt_ctrl 1100;

BUF attrib_read_buffer(.I(read), .O(read_buf));                    //This buffer taps the clock net used to distribute the AttributeRead signal

//Due to limitations in the Compact Flash Standard, addr[3] is used as a select signal between the emulated parallel port and the Pico interface
//Address[3] must be "0" for Parallel Port Accesses
//--------Host -> LPT---------//
always @(posedge write) begin
    if ((addr[1:0] == `LPT_DATA_ADDRESS) && (enable)) lpt_data[7:0] <= din[7:0];
	 if ((addr[1:0] == `LPT_CTRL_ADDRESS) && (enable)) lpt_ctrl[3:0] <= lpt_data[3:0];
end

//--------LPT -> Host---------//
assign dout[7:0] = (read_buf)?(dout_data | dout_status | dout_ctrl) : 8'b0; // ORing the databusses results in more efficient slice utilization (rather than muxes)
assign dout_data[7:0] = ((addr[1:0] == `LPT_DATA_ADDRESS) && (enable)) ? lpt_data[7:0] : 8'b0;
assign dout_status[7:0] = ((addr[1:0] == `LPT_STATUS_ADDRESS) && (enable)) ? lpt_stat_out[7:0] : 8'b0;
assign dout_ctrl[3:0] = ((addr[1:0] == `LPT_CTRL_ADDRESS) && (enable)) ? lpt_ctrl[3:0] : 4'b0;
assign dout_ctrl[7:4] = 4'b0;
assign dout[15:8] = dout[7:0];                                          // 8 Bit interfaces must mirror dataout on both bytes

assign prog_line = lpt_data[4];
assign comb_line = lpt_data[6];

assign lpt_stat_out[0] = 0;                                             // Reserved
assign lpt_stat_out[1] = 0;                                             // Reserved
assign lpt_stat_out[2] = 0;                                             // IRQ [Disabled]
assign lpt_stat_out[3] = 1;                                             // nError   (if lpt_data[4] is low, nError becomes low (active))
assign lpt_stat_out[4] = prog_line & tdo;                               // Select
assign lpt_stat_out[5] = comb_line;                                     // Paper Out
assign lpt_stat_out[6] = 1;                                             // nAck
assign lpt_stat_out[7] = ~comb_line;                                    // ~ Busy


assign din_line  = lpt_data[0];
assign ctrl_line = ~lpt_data[3];                                        // active low
assign clk_line  = lpt_data[1];
assign tms_line  = lpt_data[2];

assign tdi = ctrl_line & din_line;                                      //JTAG Signals
assign tck = ctrl_line & clk_line;
assign tms = ctrl_line & tms_line;

endmodule